﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestApp
{
    public partial class FindForm : Form
    {
        string findText = "";
        RichTextBoxFinds stringComparison;
        Form1 mainForm;
        int beginningIndex = 0;
        bool directionDown = true;


        public FindForm(Form1 newMainForm)
        {
            mainForm = newMainForm;
           
            InitializeComponent();
            button1.Enabled = false;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            findText = textBox1.Text;
            if (findText.Length != 0)
            {
                button1.Enabled = true;
            }
            else
            {
                button1.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            mainForm.Focus();
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int index = mainForm.getRichTextBox().Find(findText,beginningIndex, stringComparison);
            if (index != -1)
            {
                mainForm.getRichTextBox().Select(index, findText.Length);
                mainForm.getRichTextBox().ScrollToCaret();
                mainForm.Focus();
                if (directionDown)
                {
                    beginningIndex = beginningIndex + findText.Length;
                    if (beginningIndex > mainForm.getRichTextBox().Text.Length)
                    {
                        beginningIndex = mainForm.getRichTextBox().Text.Length;
                    }
                }
                else
                {
                    beginningIndex = beginningIndex - findText.Length;
                    if (beginningIndex < 0)
                    {
                        beginningIndex = 0;
                    }
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                stringComparison = RichTextBoxFinds.MatchCase;
            }
            else
            {
                stringComparison = RichTextBoxFinds.None;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                directionDown = false;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                directionDown = true;
            }
        }
    }
}
